/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cluster

import (
	"github.com/spf13/cobra"
	"satellite/app/cluster"
	"satellite/pkg/errorx"
	"time"
)

var Cmd = clusterCmd

// clusterCmd represents the clusterCmd command
var clusterCmd = &cobra.Command{
	Use:   "cluster",
	Short: "操作mgr的cluster资源",
	Long:  `操作mgr的cluster资源`,
	Run: func(cmd *cobra.Command, args []string) {
		_ = cmd.Help()
	},
}
var (
	baseOption = new(cluster.BaseOption)
	base       = cluster.NewBase(baseOption)
)

func init() {
	clusterCmd.Flags().StringVarP(&baseOption.Name, "name", "", "", "资源名称")
	clusterCmd.Flags().StringVarP(&baseOption.Namespace, "namespace", "n", "", "资源所在命名空间")
	clusterCmd.Flags().DurationVarP(&baseOption.Timeout, "timeout", "", 10*time.Second, "资源超时")
	clusterCmd.Flags().StringVarP(&baseOption.Type, "type", "", "auto", "资源类型,auto优先尝试 mysql ,可选: [auto,mysql,mgr]")
	createCmd.Flags().AddFlagSet(clusterCmd.Flags())
	deleteCmd.Flags().AddFlagSet(clusterCmd.Flags())
	showCmd.Flags().AddFlagSet(clusterCmd.Flags())
	restartCmd.Flags().AddFlagSet(clusterCmd.Flags())
	stopCmd.Flags().AddFlagSet(clusterCmd.Flags())
	startCmd.Flags().AddFlagSet(clusterCmd.Flags())
}

func Args(cmd *cobra.Command, args []string) error {
	if baseOption.Name == "" && len(args) == 0 {
		return errorx.MissNameErr
	}
	if baseOption.Name == "" {
		baseOption.Name = args[0]
	}
	if baseOption.Type != cluster.TypeAuto && baseOption.Type != cluster.TypeMysql && baseOption.Type != cluster.TypeMgr {
		return cluster.ErrOfInvalidType
	}
	baseOption.Fill()
	return nil
}
